<?php


namespace app\index\controller;


use app\BaseController;
use app\index\model\Category;
use app\index\model\Article;
use app\index\model\Info;
use think\exception\HttpResponseException;

class IndexController extends BaseController {
    protected $header = null;
    protected $banner = null;
    protected $articleId = 0;
    protected $categoryName = '首页';
    protected $category = [
        'zhongDianTuiJian' => ['category_name' => '重点推荐', 'category_id' => 11],
        'caiGouGongGao' => ['category_name' => '采购公告', 'category_id' => 12],
        'changSuoFuWu' => ['category_name' => '场所服务', 'category_id' => 16],
        'kuangChanPinJiaoYI' => ['category_name' => '矿产品交易', 'category_id' => 19],
        'qiTaGongGao' => ['category_name' => '其他公告', 'category_id' => 23],
    ];
    protected $infoObj = null;
    protected $info = null;

    public function initialize() {
        parent::initialize(); // TODO: Change the autogenerated stub
        $this->getBarAndBanner();
        $this->getArticleId();
        $this->getCategoryName();
        $this->getInfo();
    }

    public function redirect(...$args) {
        throw new HttpResponseException(redirect(...$args));

    }

    public function getInfo() {
        $this->infoObj = new Info();
        $this->info = stdClassToArray($this->infoObj->getInfo());
    }

    public function getArticleId() {
        if (!$this->request->isGet()) {
            return show(config('status.error'), '请求方法不对');
        }
        $this->articleId = $this->request->param('id', 0, 'intval');
        return true;
    }

    public function getCategoryName() {
        if (!$this->request->isGet()) {
            return show(config('status.error'), '请求方法不对');
        }
        $this->categoryName = $this->request->param('category_name', '首页');
        return true;
    }

    public function getBarAndBanner() {
        try {
            $this->header = (new Category())->getHeaderList();
            $this->banner = (new Article())->getBannerImages();
        } catch (\Exception $e) {
            return show(config('status.success'), $e->getMessage());
        }
        return true;
    }
}